home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
PASCAL
/
0195.ZIP
/
GETINTGR.LIB
< prev
next >
Wrap
Text File
|
1984-12-18
|
3KB
|
76 lines
{@@@@@@@@@@@ copyright (C) 1984 by Neil J. Rubenking @@@@@@@@@@@@@@@@@@@@@@@@
The purchaser of these procedures and functions may include them in COMPILED
programs freely, but may not sell or give away the source text.
Use this function to take an integer as input without risking
the user crashing your program by entering an invalid value.
You specify the allowable range.
{@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
function GetInteger(min, max : integer):integer;
var
onedigit : char;
temp : real;
good, minus, done : boolean;
PosX, PosY, digits : byte;
begin
PosX := WhereX; PosY := WhereY; { hold the starting location -- if the
person entering data makes an error,
come back to this location and blank
out six character spaces.}
repeat
GotoXY(PosX,PosY);
Write(' ');
gotoXY(PosX,PosY);
digits := 0;
temp := 0;
good := true;
done := false;
repeat
repeat until keypressed; { take characters from the keyboard }
read(Kbd, onedigit);
digits := digits + 1;
if digits = 1 then { if the FIRST character is a "-" }
if OneDigit = '-' then { then the number is negative }
begin
minus := true;
Write('-');
end
else minus := false;
case OneDigit of
'0'..'9' : begin
temp := 10*temp + ord(OneDigit) - 48;
write(OneDigit); { if the key pressed is a }
end; { number, write it on the }
#8 : begin { screen and add it to }
Write(#8,#32,#8); { "temp". If it's a Back }
temp := trunc(temp) div 10; { Space, blank out the last }
digits := digits - 2; { digit and remove it from }
end; { "temp". If it's <return> }
#13 : done := true; { you're done. }
end;
if digits > 6 then done := true; { integers can be up to 32,767,
a maximum of five digits, plus
one for a possible minus sign.}
if minus then temp := -temp; { make temp negative for comparisons }
if (temp < min) and (done or minus) then
begin
good := false;
done := true;
end;
if (temp > max) and (done or (not minus)) then
begin
good := false;
done := true;
end;
if minus then temp := - temp; { set temp back to positive for adding
more digits.}
until done;
until good and done;
if minus then temp := -temp;
GetInteger := trunc(temp);
end;
{@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}